package com.example.bootcurriculumproject.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.bootcurriculumproject.dto.ShipDto;
import com.example.bootcurriculumproject.entity.Goods;
import com.example.bootcurriculumproject.entity.Orders;
import com.example.bootcurriculumproject.mapper.GoodsMapper;
import com.example.bootcurriculumproject.mapper.OrderMapper;
import com.example.bootcurriculumproject.service.GoodsService;
import com.example.bootcurriculumproject.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.thymeleaf.util.StringUtils;

import java.util.List;

@Service
@Slf4j
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService{

    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private OrderService orderService;
    //插入1条商品数据--返回1时说明插入成功
    public int addOne(Goods goods){
        return goodsMapper.insert(goods);
    }
    //插入多条商品数据--返回1时说明插入成功
    public int addAll(List list){
       return this.addAll(list);
    }

    @Override
    public boolean update(Goods goods) {
        boolean returnNum = false;
        String id = goods.getId();
        LambdaQueryWrapper<Goods> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Goods::getId,goods.getId());
            returnNum = this.update(goods,queryWrapper);
        return returnNum;
    }

    @Override
    public boolean delete(String no) {
        LambdaQueryWrapper<Goods> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(Goods::getId,no);
        return this.remove(queryWrapper);
    }

    @Override
    public List findByName(String name) {
        LambdaQueryWrapper<Goods> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Goods::getName,name);
        List data = goodsMapper.selectList(queryWrapper);
        return data;
    }

    @Override
    public List findByAll() {
        List data = this.list();
        return data;
    }

    @Override
    public List findGoodsByType(String type) {
        LambdaQueryWrapper<Goods> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Goods::getType,type);
        List list = this.list(queryWrapper);
        return list;
    }

    //对单个商品直接购买提交订单
    @Override
    @Transactional
    public boolean submitOneGoods(ShipDto shipDto) {
        //传参要求：购买数量(buynumber) ，商品id(id) , 地址编号(addressnum) , 商品库存
        Orders orders = new Orders();
        orders.setGoodno(shipDto.getId());
        orders.setBuynumber(shipDto.getBuynumber());
        orders.setAddressnum(shipDto.getAddressnum());
        orders.setStates(0);
        orderService.save(orders);
        log.info("插入成功");
        //更改商品库存
        int number = goodsMapper.selectAmount(shipDto.getId())-shipDto.getBuynumber();
        log.info("number::::"+String.valueOf(number));
        goodsMapper.updateForByNum(number,shipDto.getId());
        return true;
    }

    public Goods getGoodsById(String goodId){
        LambdaQueryWrapper<Goods> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Goods::getId,goodId);
        Goods goods = this.getOne(queryWrapper);
        return goods;
    }
}
